package client.states;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;

import client.DoublyLinkedList;
import client.GraphicsLoader;
import client.IGameState;
import client.Link;
import client.Main;
import client.Player;
import client.StateContext;
import client.commands.ISubCommand;
import client.commands.SaveGameCommand;
import client.commands.ViewCreaturesCommand;
import client.commands.ViewEquipmentCommand;
import client.commands.ViewSpellsCommand;
import client.commands.ViewWorldCommand;

import com.jpemartin.jgt.KeyboardStatus;

public class PauseScreen implements IGameState {

	private DoublyLinkedList commands;
	private Link selectedCommand;
	private Player player;
	private int selectedIndex = 0;
	private ExploreScreen exploreScreen;
	private IGameState subState;
	// private Image shadowMask;
	private Color selectColor;

	public PauseScreen(Player player, ExploreScreen exploreScreen) {
		// shadowMask = loader.loadImage("Mask65Percent.png");
		selectColor = new Color(0x00FF00);
		commands = new DoublyLinkedList();
		this.player = player;
		this.exploreScreen = exploreScreen;
		ISubCommand viewCreatures = new ViewCreaturesCommand(player);
		commands.insertFirst(viewCreatures);
		ISubCommand viewEquipment = new ViewEquipmentCommand(player);
		commands.insertLast(viewEquipment);
		ISubCommand viewSpells = new ViewSpellsCommand(player);
		commands.insertLast(viewSpells);
		ISubCommand viewWorld = new ViewWorldCommand(exploreScreen.getWorld(), player);
		commands.insertLast(viewWorld);
		ISubCommand saveGame = new SaveGameCommand(player);
		commands.insertLast(saveGame);
		selectedCommand = commands.head();
	}

	@Override
	public void paint(Graphics g, StateContext stateContext, int GameTime) {
		g.setFont(GraphicsLoader.getLargeFont());
		if (subState == null) {
			g.setColor(Color.WHITE);
		} else {
			g.setColor(Color.WHITE.darker().darker());
		}
		g.fillRect(0, 0, Main.staticWidth(), Main.staticHeight());
		if (subState == null) {
			g.setColor(selectColor);
		} else {
			g.setColor(selectColor.darker().darker());
		}
		
		g.fillRect(0, selectedIndex * 50, 200, 50);
		g.setColor(Color.BLACK);

		int y = 0;
		if (commands != null) {
			Link current = commands.head();
			while (current != null) {
				g.setColor(Color.BLACK);
				g.drawRect(0, y, 200, 50);
				g.drawString(current.dData.toString(), 5, y + 25);
				y += 50;
				current = current.next;
			}
		}
		// subStates paint over existing screen contents to signify the game is
		// still paused
		if (subState != null) {
			// this causes bad flickering
			// g.drawImage(shadowMask, 0, 0, null);
			subState.paint(g, stateContext, GameTime);
		} else {
			g.drawString("Game Paused", 250, 25);
		}

		// g.drawString("Change Mana Color", 5, 165);
	}

	@Override
	public void processKeyHit(StateContext stateContext, KeyboardStatus status) {
		if (status.key[KeyEvent.VK_ENTER]) {
			status.key[KeyEvent.VK_ENTER] = false;
			stateContext.setState(exploreScreen);
		}
		if (subState == null) {
			if (status.key[KeyEvent.VK_RIGHT]) {
				if (selectedIndex < commands.size()) {
					selectedIndex++;
					selectedCommand = selectedCommand.next;
					status.key[KeyEvent.VK_RIGHT] = false;
				}
			} else if (status.key[KeyEvent.VK_DOWN]) {
				if (selectedIndex < commands.size()) {
					selectedIndex++;
					selectedCommand = selectedCommand.next;
					status.key[KeyEvent.VK_DOWN] = false;
				}
			} else if (status.key[KeyEvent.VK_LEFT]) {
				if (selectedIndex > 0) {
					selectedIndex--;
					selectedCommand = selectedCommand.previous;
					status.key[KeyEvent.VK_LEFT] = false;
				}
			} else if (status.key[KeyEvent.VK_UP]) {
				if (selectedIndex > 0) {
					selectedIndex--;
					selectedCommand = selectedCommand.previous;
					status.key[KeyEvent.VK_UP] = false;
				}
			} else if (status.key[KeyEvent.VK_SPACE]) {
				status.key[KeyEvent.VK_SPACE] = false;
				ISubCommand comm = (ISubCommand) selectedCommand.dData;
				comm.execute(stateContext);
				if (comm.getSubState() != null) {
					subState = comm.getSubState();
				}
			}
			status.reset();
		} else {
			subState.processKeyHit(stateContext, status);
		}

		if (status.key[KeyEvent.VK_CONTROL]) {
			status.key[KeyEvent.VK_CONTROL] = false;
			subState = null;
		}
	}

}
